package com.lyy.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;

import com.lyy.yygh.hosp.repository.DepartmentRepository;
import com.lyy.yygh.hosp.service.DepartmentService;
import com.lyy.yygh.model.hosp.Department;
import com.lyy.yygh.vo.hosp.DepartmentQueryVo;
import com.lyy.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.swing.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentRepository departmentRepository;

    /**
     * 保存科室
     * @param paramap
     */
    @Override
    public void save(Map<String, Object> paramap) {
        //将map转化为department对象,先将它转化成字符串
        String s = JSONObject.toJSONString(paramap);
        Department department = JSONObject.parseObject(s, Department.class);
        //根据医院编号和科室编号进行查询
        Department departmentexist = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentexist != null) {
            departmentexist.setUpdateTime(new Date());
            departmentexist.setIsDeleted(0);
            departmentRepository.save(departmentexist);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            departmentRepository.save(department);
        }
    }

    /**
     * 科室分页
     * @param page
     * @param limit
     * @param departmentQueryVo
     * @return
     */
    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {

        //创建page对象，每页记录数和当前页
        Pageable pageable = PageRequest.of(page - 1, limit);
        //创建example对象
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        department.setIsDeleted(0);
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);

        Page<Department> all = departmentRepository.findAll(example, pageable);
        return all;


    }

    /**
     * 删除科室
     * @param hoscode
     * @param depcode
     */
    @Override
    public void remove(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }

    }

    /**
     * 查询所有科室
     * @param hoscode
     * @return
     */
    @Override
    //跟据医院编号，查询所有科室列表
    public List<DepartmentVo> findDeptTree(String hoscode) {
        //创建List集合，封装最终数据
        List<DepartmentVo> list=new ArrayList<>();
        Department department=new Department();
        department.setHoscode(hoscode);
        Example example=Example.of(department);
        List<Department> departmentList = departmentRepository.findAll(example);
        //跟据大科室编号，bigcode，根据bigcode分组，每个大科室下有各种小科室
        Map<String, List<Department>> departmentMap =
                departmentList.stream()
                        .collect(Collectors.groupingBy(Department::getBigcode));
        //遍历department map集合
        for(Map.Entry<String, List<Department>> entry:departmentMap.entrySet()){
            //大科室编号key
            String key = entry.getKey(); //大科室编号
            //大科室所有的数据value
            List<Department> value = entry.getValue();
            //大科室
            DepartmentVo departmentVo1=new DepartmentVo();
            //拿到大科室的编号
            departmentVo1.setDepcode(key);
            //拿到大科室的名字
            departmentVo1.setDepname(value.get(0).getDepname());
            //小科室
            List<DepartmentVo> children=new ArrayList<>();
            //从value中遍历出
            for(Department department1:value){
                DepartmentVo departmentVo2=new DepartmentVo();
                departmentVo2.setDepcode(department1.getDepcode());
                departmentVo2.setDepname(department1.getDepname());
                //放到集合中
                children.add(departmentVo2);
            }
            //把小科室放到大科室中
            departmentVo1.setChildren(children);
            list.add(departmentVo1);
        }
        return list;

    }

    /**
     * 根据医院编号和科室编号得到科室名字
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if(department != null) {
            return department.getDepname();
        }
        return null;
    }

    /**
     * 根据医院编号和科室编号得到科室对象
     * @param hoscode
     * @param depcode
     * @return
     */
    @Override
    public Department getDepartment(String hoscode, String depcode) {
        return departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
    }


}
